//
//  TSTaskTableView.m
//  chart
//
//  Created by 曾斌 on 16/3/24.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSTaskSuggestTableView.h"

#import "TSRecieveMsgTableView.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"
#import "TSMsgEditTableViewController.h"
#import "TSHomeViewController.h"
#import "TSSuggestListModel.h"
#import "SuggestItemCell.h"
#import "TSTaskIngDetailController.h"
#import "TSAuditTaskDetailController.h"
@implementation TSTaskSuggestTableView{
    TSSuggestListModel * suggestListModel;
    NSArray * suggestList;
}
@synthesize requestUrl,detailType,extData;
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
    }
    suggestListModel =[[TSSuggestListModel alloc] init];
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *cell_head_identify = @"cellIdentify_head";
        SuggestItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_head_identify];
        if (!cell) {
            cell = [[SuggestItemCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_head_identify isHeader:true];
        }
        return cell;
    }
    else{
        static NSString *cell_identify = @"cellIdentify";
        SuggestItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
        if (!cell) {
            cell = [[SuggestItemCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
        }
        
        if (suggestList) {
            TSSuggestModel *model = [suggestList objectAtIndex:indexPath.row-1];
            [cell setModel:model];
        }
        
        return cell;
    }
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (suggestList) {
        return [suggestList count] + 1;
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        return;
    }
    
    if (suggestList) {
        TSSuggestModel *model = [suggestList objectAtIndex:indexPath.row-1];
    }
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    suggestListModel.requestParams = [self createRequestParam];
    [suggestListModel fetchWithPath:self.requestUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (suggestListModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->suggestList = suggestListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

-(NSMutableDictionary *)createRequestParam{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:10];
    [dic setObject:@(self.pageIndex) forKey:TSDictionaryKeyDefine_PageIndex];
    [dic setObject:@(self.pageSize) forKey:TSDictionaryKeyDefine_PageSize];
    if(extData){
        [dic addEntriesFromDictionary:extData];
    }
    return dic;
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    suggestListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [suggestListModel fetchWithPath:self.requestUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (suggestListModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->suggestList = suggestListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}



@end

